package pt.nmfa.peste.client;

import pt.nmfa.peste.client.bean.DeviceActionsDO;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.grid.events.EditCompleteEvent;
import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
import com.smartgwt.client.types.ImageStyle;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ImageViewer implements EntryPoint {
	
	public static final ListGrid actionsGrid = new ListGrid();
	public static final ListGrid deviceActionsGrid = new ListGrid();
	public static final ListGrid actionParamGrid = new ListGrid();
	
	public void onModuleLoad() {
			
			RootPanel rootPanel = RootPanel.get();
		
			final ImgButton imgButton = new ImgButton();
			imgButton.setSize("91px", "19px");
			imgButton.setImageType(ImageStyle.NORMAL);
			imgButton.setShowTitle(true);
			imgButton.setTitle("Import Lirc Config");
			imgButton.setShowDisabled(true);
			imgButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {			
				public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
					int deviceId=actionsGrid.getSelectedRecord().getAttributeAsInt("deviceId");
					String file=Window.prompt("enter file path and name", "/home/pt012275/data/Home/tv.conf");
					DeviceManagementClient.importLircConfig(deviceId,file);
					displayMessage();
				}
			});

			ListGridField employeeIdField = new ListGridField("type");
	        employeeIdField.setWidth("25%");
	        ListGridField nameField = new ListGridField("name");
	        ListGridField actionNameField = new ListGridField("name");
	        ListGridField actionTypeField = new ListGridField("actionType");
	        ListGridField parameterNameField = new ListGridField("name");
	        ListGridField parameterValueField = new ListGridField("value");

	        
			actionsGrid.setSize("536px", "405px");
	        actionsGrid.setWidth(300);
	        actionsGrid.setHeight(224);
	        actionsGrid.setAutoFetchData(false);
	        actionsGrid.setCanEdit(true);
	        actionsGrid.setEditOnFocus(false);
	        actionsGrid.setEditByCell(true);
	        actionsGrid.setFields(employeeIdField, nameField);
	        actionsGrid.setData(DeviceManagementClient.getDevices());
	        
	        actionsGrid.addEditCompleteHandler(new EditCompleteHandler() {				
				public void onEditComplete(EditCompleteEvent event) {
					DeviceManagementClient.updateDeviceByMap(event.getOldRecord(),event.getNewValues());
				}
			});
	        
	        actionsGrid.addCellClickHandler(new CellClickHandler() {
				
				public void onCellClick(CellClickEvent event) {
					imgButton.setShowDisabled(false);
					deviceActionsGrid.setData(DeviceManagementClient.getDeviceActionList(event.getRecord().getAttributeAsInt("deviceId")));
					deviceActionsGrid.show();
					actionParamGrid.hide();
				}
			});
	        
	        deviceActionsGrid.setSize("436px", "224px");
	        deviceActionsGrid.setWidth(500);
	        deviceActionsGrid.setHeight(224);
	        deviceActionsGrid.setAutoFetchData(false);
	        deviceActionsGrid.setFields(actionNameField,actionTypeField);        
	        deviceActionsGrid.hide();
	        deviceActionsGrid.addCellClickHandler(new CellClickHandler() {
				
				public void onCellClick(CellClickEvent event) {
					imgButton.setShowDisabled(false);
					actionParamGrid.setData(DeviceManagementClient.getDeviceActionParameterList(event.getRecord().getAttributeAsInt("deviceActionsId")));
					actionParamGrid.show();
				}
			});
	        

	        actionParamGrid.setSize("182px", "224px");
	        actionParamGrid.setAutoFetchData(false);
	        actionParamGrid.setFields(parameterNameField, parameterValueField);
	        actionParamGrid.hide();
		

		
			rootPanel.add(actionsGrid, 42, 72);
			rootPanel.add(deviceActionsGrid,400 , 72);
			rootPanel.add(actionParamGrid, 928, 72);
			rootPanel.add(imgButton, 47, 37);
	}


	
	public static void insertDeviceAction() {
		// TODO Auto-generated method stub
		DeviceActionsDO deviceDO=new DeviceActionsDO() ;
		deviceDO.setDeviceId(1);
		deviceDO.setName("tecla 2");
		deviceDO.setActionType("IR");
		DeviceManagementClient.createDeviceAction(deviceDO);
		
	}
	

	
	private void displayMessage()
	{
		if(!DeviceManagementClient.message.trim().equals(""))
			Window.alert(DeviceManagementClient.message);
	}
}
